package com.example.mobile2021606127cxz0508;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.mobile2021606127cxz0508.fragment.BulletinsFragment;
import com.example.mobile2021606127cxz0508.fragment.NewsFragment;
import com.example.mobile2021606127cxz0508.fragment.NoticesFragment;
import com.example.mobile2021606127cxz0508.fragment.ViewPager2Adapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout tablayout;
    private ViewPager2 viewpager2;
    List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        fragmentList.add(NewsFragment.newInstance("新闻", ""));
        fragmentList.add(NoticesFragment.newInstance("通知", ""));
        fragmentList.add(BulletinsFragment.newInstance("公告", ""));

        ViewPager2Adapter adapter = new ViewPager2Adapter(getSupportFragmentManager(), getLifecycle(), fragmentList);
        viewpager2.setAdapter(adapter);

        new TabLayoutMediator(tablayout, viewpager2, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(fragmentList.get(position).getArguments().getString("title"));
            }
        }).attach();
    }

    private void initView() {
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        viewpager2 = (ViewPager2) findViewById(R.id.viewpager2);
    }
}